package com.baiwei.vpnapi.controller;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: vpnapi
 * @description: 老api处理
 * @author: Mr. Stone
 * @create: 2020-03-25 00:33
 **/
//@RestController
//@RequestMapping("/{module}")
public class OldApiController {

    @Value("${smsconfig.url}")
    private String smsUrl;

    @Value("${smsconfig.account}")
    private String smsAccount;

    @Value("${smsconfig.apikey}")
    private String smsApikey;

    @Value("${smsconfig.sender}")
    private String smsSender;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping("/{function}")
    public Object oldHanlder(@PathVariable String module ,@PathVariable String function, HttpServletRequest request) {
        String sModule = module.toLowerCase();
        String sFunction = function.toLowerCase();
        System.out.println(sModule + "||" + sFunction);
        if("apiv2".equals(sModule) && "getspeedupversion".equals(sFunction)) {
            String platfrom  = request.getParameter("Type");
            Map<String, String> versionInfo = new HashMap<>();
            versionInfo.put("UpdateType", "1");
            if(platfrom.equals("Windows")) {
                versionInfo.put("Version", "2.4.2");
            } else {
                versionInfo.put("Version", "23");
            }
            ArrayList<Map<String,String>> vCon = new ArrayList<>();
            vCon.add(versionInfo);
            Map<String, Object> res = new HashMap<>();
            res.put("result", 0);
            res.put("data", vCon);
            return res;
        } else if("apiv2".equals(sModule) && ("userlogin".equals(sFunction) || "usercreate".equals(sFunction))) {
            String cityCode = request.getParameter("CellPhoneCode");
            String phone = request.getParameter("CellPhone");
            String pwd = request.getParameter("Password");
            String regPhone = cityCode + phone;
            System.out.println(sFunction + " : " + regPhone);
            sendAuthCodeSms(regPhone);
            return null;
        } else {
            Map<String, String> res = new HashMap<>();
            res.put("result", "10001");
            res.put("msg", "Expired token");
            return res;
        }
    }


    private Object sendAuthCodeSms(String phone) {
        String tableHeader = "trans";
        if(stringRedisTemplate.opsForValue().get(tableHeader + "." + phone) == null) {
            stringRedisTemplate.opsForValue().set(tableHeader + "." + phone, "1", 1, TimeUnit.DAYS);
            RestTemplate rest = new RestTemplate();
            String url = smsUrl + "?"
                    + "account_name=" + smsAccount + "&"
                    + "api_key=" + smsApikey + "&"
                    + "sender=" + smsSender + "&"
                    + "mobile=" + phone + "&"
                    + "sms_content=" + "由于系统升级请到官网下载最新版本：https://ponyspeedup.com/zh-CN/download";
            String result = rest.getForObject(url, String.class);
            ObjectMapper mapper = new ObjectMapper();
            try {
                Map<String, Object> res = mapper.readValue(result, Map.class);
                Integer sta = (Integer)(res.get("status"));
                if(sta == 1) {
                    return "success";
                } else {
                    throw new RuntimeException("验证码发送错误");
                }
            } catch (JsonParseException e) {
                throw new RuntimeException("验证码发送错误");
            } catch (JsonMappingException me) {
                throw new RuntimeException("验证码发送错误");
            } catch (IOException ie) {
                throw new RuntimeException("验证码发送错误");
            }
        }
        return null;
    }


}
